<?php
class Third_Form_Pattern
{
	protected $field, $name, $options, $translate, $controller, $validation;

	public function __construct( $name , $options ){
		
		$this->name  	= $name;
		$this->options 	= $options;
		$this->field 	= new Zend_Form_Element( $this->name );
		
		$this->view  = Zend_Registry::get('view');
		//$this->controller = Zend_Registry::get('controller');
	}

	function get( $handle = false){

		if( !$handle )
			$handle = $this->options['display'];
		else
			$handle = $this->options['editable'];

		if( $handle  == 'true' ){

			$this->setPattern( $this->options['pattern'] );

			$this->setValidation();

			$this->setAction();

			if ( $this->field->isRequired() ) {
	            $this->field->setLabel( $this->options['label']." (*) " );
	        } else {
	        	$this->field->setLabel( $this->options['label'] );
	        } 

	        $this->field->setAttrib('size', $this->options['size'] );
	        
			return $this->field;
		}
		return false;
	}
	
	function addValidation( $index, $value ){
		if( $index )
			$this->validation["validate"]["$index"] = $value;
	}

	function setValidation(){
		if( $this->options['component'] != 'hidden' ){

			//VALIDACAO DE CAMPO OBRIGATORIO
			if($this->options['required'] == 'true'){
					$this->field->setRequired(true);
					$this->addValidation('required', true );
			}
			
			// VALIDACAO DE EMAIL
			if($this->options['validation_type'] == 'email'){
					$this->field->addValidator('EmailAddress', false);
					$this->addValidation('email', true );
			}
	
			// VALIDACAO DE CAMPOS QUE SAO UNIQUE
			if( $this->options['unique'] == "true" ){
					$message = array();
					if( !empty( $this->options['message'] ) )
						$message = array( 'recordFound' => $this->options['message'] );
	
						// $this->field->addErrorMessages( $message );
				
					$this->field->addValidator('Db_NoRecordExists', true, array( $this->options['table'], $this->name ) ); //, $this->name);
			}
	
			// VERIFICA SE ITEM ESTA CADASTRADO
			if( $this->options['unique'] == "exists" ){
					$this->field->addValidator('Db_RecordExists', true, array( $this->options['table'], $this->name ) ); //, $this->name);
			}
			
			// TAMANHO DA STRING
			if( !empty( $this->options['minlength'] ) ){
				
					$range[] = $this->options['minlength'];
					
					if( !empty($this->options['maxlength']) )
						$range[] = $this->options['maxlength'];
					
					$this->field->addValidator( 'StringLength', false, $range );
			}
	
			// ALFANUMERICO SEM ESPACOS EM BRACO
			if( $this->options['alnumws'] == "true" ){
					$this->field->addValidator('Regex', true, array("/^([A-Za-z0-9_])+$/") );
					$this->field->addFilter('StringToLower');
					$this->field->addFilter('StringTrim');
					$this->field->getValidator('Regex')->setMessage( "\"%value%\" cont&eacute;m caracteres e n&uacute;meroso alfa-num&eacute;ricos. S&oacute; s&atilde;o permitidas letras, n&uacute;meros e sublinhados" ,  Zend_Validate_Regex::NOT_MATCH);
			}
	
			// VALIDA CAMPOS QUE DEVEM SER IGUAIS
			if( !empty( $this->options['equalTo'] ) ){
					$message = array();
					if( !empty( $this->options['message'] ) ){
						$message = array( 'notSame' => $this->options['message'] );
						$this->field->addErrorMessages( $message );
					}
	
					$this->field->addValidator('Identical', true, array( $this->controller->getRequest()->getParam( $this->options['equalTo'] ) ) );
			}
	
			//ADICIONA CLASS PARA VALIDACAO VIA JAVASCRIPT
			
				$classValidate = str_replace('"','',Zend_Json::encode( $this->validation ) );
				$this->field->setAttrib('class', $classValidate );
		}
	}
	
	function setAction(){
		$actions = array( 'onFocus', 'onBlur', 'onClick', 'onChange', 'onKeyPress', 'accessKey');
		foreach( $actions as $act ){
			if(!empty( $this->options[ $act ] ))
				$this->field->setAttrib($act, $this->options[ $act ] );
		}
	}
	
	function setPattern( $pattern ) {
		$pattern = strtolower( $pattern );

		switch( $pattern ){
			case 'enum':
				// $this->options['values'] = array("Sim"=>"S", "Nao"=>"N" );
				
				$values = Zend_Json::decode( $this->options['values'] );
				if(is_array( $values ))
					$this->field->addMultiOptions( $values );
			break;
			
			case 'sex':
			    $sexo = array('M'=>'Masculino', 'F'=>'Feminino');
			    $this->field->setMultiOptions( $sexo )->setSeparator('');
			break;
		}
	}
}
?>